//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2006
//
// Set indexed city attribute for object.
//

# include "StdAfx.h"
# include "SetIndexedCity.h"
# include "UndoRedo.h"
# include "Label.h"

class CSetIndexedCityAction : public CEditAction {
  public:
	CSetIndexedCityAction (rgn_t & _rgn, bool _bIndexedCity) :
		m_rgn (_rgn),
		m_bNewIndexedCity (_bIndexedCity),
		m_bOldIndexedCity ((_rgn.dwFlags & rgn_t::fIndexedCity) != 0),
		m_bWasModified ((_rgn.dwFlags & rgn_t::fModified) != 0)
	{}

	virtual void GetName (string_t & _str) const;
	virtual cu_t CanUndo () const {return cuYes;}
	virtual bool Do ();
	virtual void Undo ();
	virtual void Redo () {CSetIndexedCityAction::Do ();}

  private:
	rgn_t &    m_rgn;
	bool const m_bNewIndexedCity;
	bool const m_bOldIndexedCity;
	bool const m_bWasModified;
};

void CSetIndexedCityAction::GetName (string_t & _str) const {
	CCritSec::CLock lock (g_cs);

	_str.append ("change indexed city [");
	GetHintText (m_rgn, _str);
	_str.append ("]");
}

bool CSetIndexedCityAction::Do () {
	CCritSec::CLock lock (g_cs);

	if (m_bNewIndexedCity)
		m_rgn.dwFlags |= rgn_t::fIndexedCity;
	else
		m_rgn.dwFlags &= ~rgn_t::fIndexedCity;

	m_rgn.dwFlags |= rgn_t::fModified;

	return true;
}

void CSetIndexedCityAction::Undo () {
	CCritSec::CLock lock (g_cs);

	if (m_bOldIndexedCity)
		m_rgn.dwFlags |= rgn_t::fIndexedCity;
	else
		m_rgn.dwFlags &= ~rgn_t::fIndexedCity;

	if (! m_bWasModified)
		m_rgn.dwFlags &= ~rgn_t::fModified;
}

///////////////////////////

void SetIndexedCity (rgn_t & _rgn, bool _bIndexedCity) {
	PerformEditAction (new CSetIndexedCityAction (_rgn, _bIndexedCity));
}
